package org.bluedream.comm.utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Arrays;
import java.util.Random;

public class EncryFor3DESUtil {
    private static final String Algorithm = "DESede"; // 定义 加密算法,可用
    private static final char[] BASE = new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

    private EncryFor3DESUtil() {
        throw new AssertionError();
    }

    /**
     * 加密
     * @param data      待加密明文
     * @param strKey    传入密钥：16位数字或字母，大小写敏感
     * @return
     * @throws Exception
     */
    public static String encrypt(String data , String strKey) throws Exception{
        data = data + "        ";
        byte[] bMsg = data.getBytes("gbk");
        int l = (bMsg.length / 16 + 1) * 16;
        byte[] btMsg = Arrays.copyOf(bMsg, l);
        byte[] digestOfPassword = strKey.getBytes();
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        int j = 0;

        for(int var8 = 16; j < 8; keyBytes[var8++] = keyBytes[j++]) {
            ;
        }

        SecretKey key = new SecretKeySpec(keyBytes, Algorithm);
        Cipher c1 = Cipher.getInstance(Algorithm);
        c1.init(Cipher.ENCRYPT_MODE, key);
        String rtn = StringUtils.byte2hex(c1.doFinal(btMsg));
        return rtn.substring(0, (bMsg.length / 8 + 1) * 16);
    }

    /**
     * 解密
     * @param message       密文
     * @param strKey        传入密钥：16位数字或字母，大小写敏感
     * @return
     * @throws Exception
     */
    public static String decrypt(String message, String strKey) throws Exception {
        message = message + getAdd(message.length(), strKey);
        byte[] digestOfPassword = strKey.getBytes();
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        int j = 0;

        for(int var5 = 16; j < 8; keyBytes[var5++] = keyBytes[j++]) {
            ;
        }

        SecretKey key = new SecretKeySpec(keyBytes, Algorithm);
        Cipher decipher = Cipher.getInstance(Algorithm);
        decipher.init(Cipher.DECRYPT_MODE, key);
        byte[] plainText = decipher.doFinal(StringUtils.hex2byte(message));
        return new String(plainText, "gbk");
    }

    private static String getAdd(int length, String strKey) throws Exception {
        byte[] btMsg = new byte[length / 2];
        byte[] digestOfPassword = strKey.getBytes("utf-8");
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        int j = 0;

        for(int var6 = 16; j < 8; keyBytes[var6++] = keyBytes[j++]) {
            ;
        }

        SecretKey key = new SecretKeySpec(keyBytes, Algorithm);
        Cipher c1 = Cipher.getInstance(Algorithm);
        c1.init(Cipher.ENCRYPT_MODE, key);
        String rtn = StringUtils.byte2hex(c1.doFinal(btMsg));
        return rtn.substring(length);
    }

    /**
     * 生成key
     * @param len
     * @return
     */
    public static String newKey(int len) {
        Random rd = new Random();
        StringBuffer sb = new StringBuffer();

        for(int i = 0; i < len; ++i) {
            sb.append(BASE[Math.abs(rd.nextInt()) % 62]);
        }
        return sb.toString();
    }

}
